﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.MyLayouts;
using System.Threading;
using QLNH.QLNH_BUS_WS;

namespace QLNH.MyForms
{
    public partial class AdminEventManagement : FormUC
    {
        #region Constructor

        public AdminEventManagement(LayoutUC mainLayout)
        {
            InitializeComponent();
            this._mainLayout = mainLayout;
            this._formKey = FormKey.ADMIN_EVENT_MANAGEMENT;
        }

        #endregion

        #region Functions

        public override void Resize(Size size)
        {
            if (this.Size.Width < size.Width)
            {
                this.Location = new Point(size.Width / 2 - this.Size.Width / 2, size.Height / 2 - this.Size.Height / 2);
            }
            else
            {
                this.Location = new Point(0, 0);
            }
        }

        //lay danh sach cac su kien
        private void GetAllEvent()
        {
            this.lv_events.Items.Clear();
            Thread t = new Thread(new ThreadStart(GetAllEventThread));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }

        private List<EventDTO> _eventList = null;

        private void GetAllEventThread()
        {
            if (_eventList == null)
            {
                QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
                _eventList = new List<EventDTO>();
                EventDTO[] temp = ws.GetAllEvent();
                _eventList = temp.ToList<EventDTO>();
            }

            List<EventDTO> events;
            if (filterChosen == 0)
                events = _eventList;
            else
                events = _eventList.FindAll(e => e.StartDate.Date >= startDate.Date && e.EndDate.Date <= endDate.Date);

            if (events.Count > 0)
            {
                for (int i = 0; i < events.Count; i++)
                {
                    ListViewItem li = new ListViewItem();
                    li.Text = events[i].ID;
                    li.Name = events[i].ID;
                    li.SubItems.Add(events[i].Description);
                    li.SubItems.Add(events[i].StartDate.ToString());
                    li.SubItems.Add(events[i].EndDate.ToString());

                    if (this.lv_events.Items.Count % 2 != 0)
                        li.BackColor = Color.LightGray;

                    this.lv_events.BeginInvoke((MethodInvoker)(() => this.lv_events.Items.Add(li)));

                    Thread.Sleep(50);
                }
            }
        }

        private void RemoveEvent(string id)
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            if (ws.RemoveEvent(id) == true)
            {
                this._eventList.Remove(this._eventList.Find(e => e.ID.Equals(id)));
                MessageBox.Show("Xóa sự kiện thành công");
                GetAllEvent();
            }
            else
            {
                MessageBox.Show("Xóa sự kiện thất bại");
            }
        }

        #endregion

        #region Control Events

        private int filterChosen = 0;
        private DateTime startDate = DateTime.Now;
        private DateTime endDate = DateTime.Now;

        private void rbt_action(object sender, EventArgs e)
        {
            int tag = Int32.Parse(((RadioButton)sender).Tag.ToString());

            DateTime now = DateTime.Now;
            switch (tag)
            {
                case 0:
                    break;
                case 1:
                    startDate = now;
                    int daysToWeekend = DayOfWeek.Saturday - now.DayOfWeek;
                    endDate = now.AddDays(daysToWeekend);
                    //endDate = endDate.Add(-now.TimeOfDay);
                    break;
                case 2:
                    startDate = now;
                    endDate = now.AddDays(DateTime.DaysInMonth(now.Year, now.Month) - now.Day);
                    //endDate = endDate.Add(-now.TimeOfDay);
                    break;
                case 3:
                    this.dtp_start.Enabled = this.dtp_end.Enabled = this.rbt_date.Checked;
                    startDate = this.dtp_start.Value;
                    endDate = this.dtp_end.Value;
                    break;
                default:
                    break;
            }

            filterChosen = tag;
            if (this.bt_filter.Enabled == false)
                this.bt_filter.Enabled = true;
        }

        private void AdminEventManagement_Load(object sender, EventArgs e)
        {
            GetAllEvent();
            this.tooltip.Show("Nhắp chuột vào đây để sắp xếp lại", this.lv_events, new Point(0, 0), 1000);
        }

        private void bt_filter_Click(object sender, EventArgs e)
        {
            this.bt_filter.Enabled = false;
            GetAllEvent();
        }

        private void lv_events_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && this.lv_events.SelectedItems.Count > 0)
            {
                this.cmt_rightClick.Show(this.lv_events, e.Location);
            }
        }

        private ListViewItemComparer sorter;
        private bool ascending = true;
        private void lv_events_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            sorter = new ListViewItemComparer(e.Column);
            sorter.OrderOfSort = (ascending == true) ? SortOrder.Ascending : SortOrder.Descending;
            this.lv_events.ListViewItemSorter = sorter;
            this.lv_events.Sort();
            ascending = !ascending;
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            this._mainLayout.SwitchForm(FormKey.ADMIN_EVENT_EDIT);
        }

        private void cmt_action(object sender, EventArgs e)
        {
            int tag = Int32.Parse(((ToolStripMenuItem)sender).Tag.ToString());
            ListViewItem li = this.lv_events.SelectedItems[0];
            switch (tag)
            {
                case 0:
                    {
                        string id = li.Name;
                        string description = li.SubItems[1].Text;
                        DateTime start = DateTime.Parse(li.SubItems[2].Text);
                        DateTime end = DateTime.Parse(li.SubItems[3].Text);

                        this._mainLayout.SwitchForm(FormKey.ADMIN_EVENT_EDIT, new object[] { id, description, start, end });
                    }
                    break;
                case 1:
                    if (MessageBox.Show("Bạn có thật sự muốn xóa sự kiện này?", "Nhắc nhở", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        string id = li.Name;
                        RemoveEvent(id);
                    }
                    break;
                default:
                    break;
            }
        }

        private void dtp_action(object sender, EventArgs e)
        {
            int tag = Int32.Parse(((DateTimePicker)sender).Tag.ToString());
            if (tag == 0)
                startDate = this.dtp_start.Value;
            else if (tag == 1)
            {
                endDate = this.dtp_end.Value;
            }
            if (this.bt_filter.Enabled == false)
                this.bt_filter.Enabled = true;
        }

        #endregion
    }
}
